#!/bin/sh
#
# gps            Power on 3g
#

start() {
    printf "Starting 3g: "
    if [ ! -d "/sys/class/gpio/gpio288/" ]
    then
        /bin/echo "288" >/sys/class/gpio/export
    fi
    /bin/echo "out" >/sys/class/gpio/gpio288/direction
    /bin/echo "1" >/sys/class/gpio/gpio288/value
    /bin/sleep 1
    /bin/echo "0" >/sys/class/gpio/gpio288/value
    /bin/sleep 8 ##waiting for attach /dev/ttyUSB*
    /bin/echo "OK"
}
stop() {
    printf "Stopping 3g: "
    /bin/echo "out" >/sys/class/gpio/gpio288/direction
    /bin/echo "1" >/sys/class/gpio/gpio288/value
    /bin/sleep 4
    /bin/echo "0" >/sys/class/gpio/gpio288/value
    /bin/echo "OK"
}
restart() {
    stop
    /bin/sleep 8
    start
    reset
}
reset() {
    printf "Reset 3g:"
    if [ ! -d "/sys/class/gpio/gpio291/" ]
    then
        /bin/echo "291" >/sys/class/gpio/export
    fi
    /bin/echo "out" >/sys/class/gpio/gpio291/direction  #PJ3  291
    /bin/echo "1" >/sys/class/gpio/gpio291/value
    /bin/sleep 0.05
    /bin/echo "0" >/sys/class/gpio/gpio291/value
    /bin/echo "OK"
}
wakeup() {
    printf "Waking up 3g:"
    if [ ! -d "/sys/class/gpio/gpio289/" ]
    then
        /bin/echo "289" >/sys/class/gpio/export
    fi
    /bin/echo "out" >/sys/class/gpio/gpio289/direction  #PJ1  289
    /bin/echo "0" >/sys/class/gpio/gpio289/value
    /bin/echo "OK"
}
sleep() {
    printf "Sleep 3g:"
    if [ ! -d "/sys/class/gpio/gpio289/" ]
    then
        /bin/echo "289" >/sys/class/gpio/export
    fi
    /bin/echo "out" >/sys/class/gpio/gpio289/direction  #PJ1  289
    /bin/echo "1" >/sys/class/gpio/gpio289/value
    /bin/echo "OK"
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        restart
        ;;
  wakeup)
        wakeup
        ;;
  sleep)
        sleep
        ;;
  reset)
        reset
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?
